package com.itheima.reggie.common;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//短信发送
@Data
@Component
@ConfigurationProperties(prefix = "reggie.sms")
public class SmsTemplate {
    private String key;
    private String secret;
    private String signName;
    private String templateCode;
    //调用阿里云发送短信
    public void sendSms(String phoneNumbers, String code){
        //设置超过多长时间失效
        System.setProperty("sun.net.client.defaultConnectTimeout","10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        try {
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",key,secret);
            DefaultProfile.addEndpoint("cn-hangzhou","cn-hangzhou","Dysmsapi","dysmsapi.aliyuncs.com");
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumbers);//手机号
            request.setSignName(signName);//短信前面的名字
            request.setTemplateCode(templateCode);//短信模板
            request.setTemplateParam("{\"code\":\""+ code+"\"}");//验证码
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
